package com.yunpeng.aidevice.dao.service.impl;

import com.yunpeng.aidevice.common.dto.save.MailSenderSaveDTO;
import com.yunpeng.aidevice.common.enums.ResultCodeEnum;
import com.yunpeng.aidevice.dao.constant.DictionariesConstant;
import com.yunpeng.aidevice.dao.mapper.record.MerchantNoticeMailMapper;
import com.yunpeng.aidevice.dao.model.record.MerchantNoticeMailDO;
import com.yunpeng.aidevice.dao.service.IMerchantNoticeMailService;
import com.yunpeng.common.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 商户通知邮箱设置 服务类实现
 * </p>
 *
 * @author hemw
 * @since 2020-05-04
 */
@Service
public class MerchantNoticeMailServiceImpl implements IMerchantNoticeMailService {

    @Resource
    private MerchantNoticeMailMapper merchantNoticeMailMapper;

    @Override
    /**
     * 更新邮箱设置
     * @param mailSenderSaveDTO 默认邮箱配置
     * @param operatorCode      操作人
     * @return
     */
    public Boolean updateMailSender(MailSenderSaveDTO mailSenderSaveDTO, String operatorCode) {
        if (mailSenderSaveDTO == null || StringUtils.isBlank(mailSenderSaveDTO.getMailHost())
                || StringUtils.isBlank(mailSenderSaveDTO.getPassword())
                || StringUtils.isBlank(mailSenderSaveDTO.getProtocol())
                || StringUtils.isBlank(mailSenderSaveDTO.getSenderEmail())
                || StringUtils.isBlank(mailSenderSaveDTO.getSenderName())
                || StringUtils.isBlank(mailSenderSaveDTO.getTenantCode())
        ) {
            throw new BizException(ResultCodeEnum.PARAMETER_CHECK_FAILED);
        }
        MerchantNoticeMailDO merchantNoticeMailDO;
        if (mailSenderSaveDTO.getDefault()) {
            //默认邮箱配置不存在新增
            merchantNoticeMailDO = merchantNoticeMailMapper.getDefaultMailConfig(mailSenderSaveDTO.getTenantCode());
            if (merchantNoticeMailDO == null) {
                return addNewMailSender(mailSenderSaveDTO, operatorCode);
            }
            mailSenderSaveDTO.setMerchantCode(null);
        } else {
            //商户邮箱配置不存在，抛异常返回
            merchantNoticeMailDO = merchantNoticeMailMapper.getMerchantMailConfig(mailSenderSaveDTO.getMerchantCode(), mailSenderSaveDTO.getTenantCode());
            if (merchantNoticeMailDO == null) {
                throw new BizException(ResultCodeEnum.NOT_EXISTS_DATA_FAILED);
            }
        }
        Map<String, Object> updateMap = new HashMap<>(16);
        if (StringUtils.isBlank(merchantNoticeMailDO.getMerchantCode())) {
            updateMap.put("merchant_code", mailSenderSaveDTO.getMerchantCode());
        }
        updateMap.put("protocol", mailSenderSaveDTO.getProtocol());
        updateMap.put("mail_host", mailSenderSaveDTO.getMailHost());
        updateMap.put("sender_email", mailSenderSaveDTO.getSenderEmail());
        updateMap.put("sender_name", mailSenderSaveDTO.getSenderName());
        updateMap.put("password", mailSenderSaveDTO.getPassword());
        updateMap.put("tenant_code", mailSenderSaveDTO.getTenantCode());
        updateMap.put("updater", operatorCode);
        updateMap.put("update_time", LocalDateTime.now());
        updateMap.put("port", mailSenderSaveDTO.getPort());
        Boolean updateResult = merchantNoticeMailMapper.update(merchantNoticeMailDO.getId(), updateMap) > 0;
        //更新本地缓存
        if (updateResult) {
            MerchantNoticeMailDO newNoticeConfig = merchantNoticeMailMapper.getById(merchantNoticeMailDO.getId());
            DictionariesConstant.INSTANCE.updateCacheNoticeMailConfig(newNoticeConfig);
        }
        return updateResult;
    }

    @Override
    /**
     * 新增邮箱发送配置
     *
     * @param mailSenderSaveDTO 邮箱发送配置
     * @param operatorCode      操作人
     * @return
     */
    public Boolean addNewMailSender(MailSenderSaveDTO mailSenderSaveDTO, String operatorCode) {
        if (mailSenderSaveDTO == null || StringUtils.isBlank(mailSenderSaveDTO.getMailHost())
                || StringUtils.isBlank(mailSenderSaveDTO.getPassword())
                || StringUtils.isBlank(mailSenderSaveDTO.getProtocol())
                || StringUtils.isBlank(mailSenderSaveDTO.getSenderEmail())
                || StringUtils.isBlank(mailSenderSaveDTO.getSenderName())
                || StringUtils.isBlank(mailSenderSaveDTO.getTenantCode())
        ) {
            throw new BizException(ResultCodeEnum.PARAMETER_CHECK_FAILED);
        }
        MerchantNoticeMailDO merchantNoticeMailDO = new MerchantNoticeMailDO();
        merchantNoticeMailDO.initForNew(operatorCode);
        if (StringUtils.isBlank(merchantNoticeMailDO.getMerchantCode())) {
            merchantNoticeMailDO.setMerchantCode(mailSenderSaveDTO.getMerchantCode());
        }
        merchantNoticeMailDO.setProtocol(mailSenderSaveDTO.getProtocol());
        merchantNoticeMailDO.setMailHost(mailSenderSaveDTO.getMailHost());
        merchantNoticeMailDO.setSenderEmail(mailSenderSaveDTO.getSenderEmail());
        merchantNoticeMailDO.setSenderName(mailSenderSaveDTO.getSenderName());
        merchantNoticeMailDO.setPassword(mailSenderSaveDTO.getPassword());
        merchantNoticeMailDO.setPort(mailSenderSaveDTO.getPort());

        if (mailSenderSaveDTO.getDefault()) {
            MerchantNoticeMailDO defaultMailConfig = merchantNoticeMailMapper.getDefaultMailConfig(mailSenderSaveDTO.getTenantCode());
            if (defaultMailConfig != null && defaultMailConfig.getId() != null) {
                throw new BizException(ResultCodeEnum.EXISTS_DATA_FAILED);
            }
            merchantNoticeMailDO.setIsDefault(true);
            merchantNoticeMailDO.setMerchantCode(null);
        } else {
            MerchantNoticeMailDO merchantMailConfig = merchantNoticeMailMapper.getMerchantMailConfig(mailSenderSaveDTO.getMerchantCode(), mailSenderSaveDTO.getTenantCode());
            if (merchantMailConfig != null && merchantMailConfig.getId() != null) {
                throw new BizException(ResultCodeEnum.EXISTS_DATA_FAILED);
            }
            merchantNoticeMailDO.setIsDefault(false);
        }
        merchantNoticeMailDO.setTenantCode(mailSenderSaveDTO.getTenantCode());
        Long newId = merchantNoticeMailMapper.add(merchantNoticeMailDO);
        //更新本地缓存
        if (newId > 0) {
            MerchantNoticeMailDO newNoticeConfig = merchantNoticeMailMapper.getById(newId);
            DictionariesConstant.INSTANCE.updateCacheNoticeMailConfig(newNoticeConfig);
            return true;
        }
        return false;
    }

    @Override
    /**
     * 获取商户的邮箱发送配置
     *
     * @param merchantCode
     * @param tenantCode
     * @return
     */
    public MerchantNoticeMailDO getMerchantNoticeMailConfig(String merchantCode, String tenantCode) {
        if (StringUtils.isBlank(merchantCode)) {
            return null;
        }
        if (StringUtils.isBlank(tenantCode)) {
            return null;
        }
        return merchantNoticeMailMapper.getMerchantMailConfig(merchantCode, tenantCode);
    }

    @Override
    /**
     * 获取默认的邮箱发送配置
     *
     * @param tenantCode
     * @return
     */
    public MerchantNoticeMailDO getDefaultNoticeMailConfig(String tenantCode) {
        if (StringUtils.isBlank(tenantCode)) {
            return null;
        }
        return merchantNoticeMailMapper.getDefaultMailConfig(tenantCode);
    }

}
